const sqlhelper = require('../sqlhelper/sqlhelper');

module.exports = {
  getUserInfoByEmailAndPassword(email, password, callback) {
    let connect = sqlhelper.getConnection();

    let sql = "select * from users where email = ? and password = ?";

    connect.query(sql, [email, password], (err, result, fields) => {
      callback(err, result);
    });
    connect.end();
  },
  getUserHeadAndNickname(id, callback) {
    let connection = sqlhelper.getConnection();
    let sql = 'SELECT avatar,nickname FROM users WHERE id = ?';
    connection.query(sql, [id], (err, result, fields) => {
      callback(err, result);
    });
  },
  getAllUser(callback) {
    let connection = sqlhelper.getConnection();
    let sql = "select * from users";
    connection.query(sql, (err, result, fields) => {
      callback(err, result);
    });
  },
  getUserByEmail(email, callback) {
    let connection = sqlhelper.getConnection();
    let sql = "select * from users where email = ?";
    connection.query(sql, [email], (err, result, fields) => {
      callback(err, result);
    });
  },
  addNewUser(data, callback) {
    let connection = sqlhelper.getConnection();
    let sql = "insert into users set ?";
    connection.query(sql, data, (err, result, fields) => {
      callback(err, result);
    });
  },
  getUserInfoById(id, callback) {
    let connection = sqlhelper.getConnection();
    let sql = "select * from users where id = ?";
    connection.query(sql, [id], (err, result, fields) => {
      callback(err, result);
    });
  },
  updateUserInfoById(id, data, callback) {
    let connection = sqlhelper.getConnection();
    let sql = "update users set ? where id = ?";
    connection.query(sql, [data, id], (err, result, fields) => {
      callback(err, result);
    });
  },
  delUserById(id, callback) {
    let connection = sqlhelper.getConnection();
    let sql = "delete from users where id = ?";
    connection.query(sql, [id], (err, result, fields) => {
      callback(err, result);
    });
  },
  delMultiple(ids, callback) {
    let connection = sqlhelper.getConnection();
    let sql = "delete from users where id in (?)";

    connection.query(sql, [ids], (err, result, fields) => {
      callback(err, result);
    });
  }
}